/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BuildData;


import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 *
 * @author Pham_Thanh
 */
public class LMData {
//    public static void main(String argv[])throws XMLStreamException, FileNotFoundException, UnsupportedEncodingException, IOException{
//        LMData lm=new LMData("test_punc_nsw_url_split_num_alpha_expn.xml");
//    }
    public LMData() throws XMLStreamException, FileNotFoundException, UnsupportedEncodingException, IOException{

        System.out.println("package BuildData; class LMData");

        FileInputStream fis = new FileInputStream("trainset\\hand\\expn_hand.xml");
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data\\lm\\lmdata.db"),"UTF8"));
        String doc="";
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if(localName.equals("record")){
                        doc="";
                    }
                    if (localName.equals("W")) {
                        String atValue = xmlStreamReader.getAttributeValue(0);
                        if (atValue.equals("EXPN")) {
                           
                                    
//                            String expn=xmlStreamReader.getElementText();
                            String full=xmlStreamReader.getAttributeValue(1);
                             if(doc.equals("")){
                                doc=doc+full;
                            }else{
                                doc=doc+"|"+full;
                            }
                            
                        }
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                 String endName=xmlStreamReader.getLocalName();
                 if(endName.equals("record")){
                     doc+="\n";
                     bw.write(doc);
                 }
            }
        }

        bw.close();
    }

}
